
package jfc.core;

import java.awt.*;
import java.awt.image.BufferedImage;
import jfc.ui.ImageManager;

/**
 *  @author Yann Le Gall
 *  ylegall@gmail.com
 *  Oct 25, 2009 1:00:21 AM
 */
public class Card extends Rectangle implements Comparable<Card> {

    private final Suit suit;
    private final int value;
    private final String displayValue;
    private final BufferedImage bufferedImage;

    private static final int WIDTH;
    private static final int HEIGHT;
    private static final Font FONT;
    public static final Dimension SIZE;

    static {
        ImageManager im = ImageManager.getInstance();
        SIZE = im.getCardSize();
        WIDTH = SIZE.width;
        HEIGHT = SIZE.height;
        FONT = new Font("Arial", Font.BOLD, 25);
    }

    public Card(int value, Suit suit)
    {
        this.suit = suit;
        this.value = value;

        switch (value)
        {
            case 1: displayValue = "A"; break;
            case 11: displayValue = "J"; break;
            case 12: displayValue = "Q"; break;
            case 13: displayValue = "K"; break;
            default: displayValue = new Integer(value).toString();
        }

        ImageManager im = ImageManager.getInstance();
        width = WIDTH; height = HEIGHT;

        bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bufferedImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setFont(FONT);

        switch (suit)
        {
            case HEARTS:
            case DIAMONDS:
                g2.setColor(Color.red); break;
            default:
                g2.setColor(Color.BLACK);
        }

        g2.drawImage(im.getCardImage().getImage(), null, null);
        Image image = im.getImage(suit).getImage();
        g2.drawImage(image, 12, 50, null);
        g2.drawImage(image, 5, 5, 25, 25, null);
        g2.drawString(displayValue, 30, 25);
        g2.dispose();
    }

    public final int getValue() { return value;}
    public final Suit getSuit() { return suit;}

    public final void moveBy(int dx, int dy) {
        x += dx;
        y += dy;
    }

    public final int compareTo(Card other) {
        if(other != null)
            return value - other.value;
        return 0;
    }

    public final boolean isSameSuit(Card other) {
        if(other != null)
            return other.suit == suit;
        return false;
    }

    public final boolean isSameColor(Card other) {
    	if(this.isSameSuit(other)) 
    		return true;
        switch (suit)
        {
            case HEARTS:
                return other.suit == Suit.DIAMONDS;
            case DIAMONDS:
                return other.suit == Suit.HEARTS;
            case CLUBS:
                return other.suit == Suit.SPADES;
            case SPADES:
                return other.suit == Suit.CLUBS;
        }
        return false;
    }

    public static final int width() { return WIDTH;}
    public static final int height() { return HEIGHT;}

    public void draw(Graphics2D g2) {
        g2.drawImage(bufferedImage, null, x, y);
    }

    @Override
    public String toString() {
        return new StringBuilder().append(suit.ordinal()).append(10+value).toString();
    }
}
